package com.climber.spb_rabbitmq.exchange.topic;

import com.climber.spb_rabbitmq.utils.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;

public class TopicConsumer1 {
    private static String EXCHANGE_NAME = "topic_logs";
    private static String QUEUE_NAME = "Q1";

    public static void main(String[] args) {
        Channel channel = RabbitMQUtils.getChannel();
        try {
//            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "q1.#");
            System.out.println("waiting for message ......");
            DeliverCallback deliverCallback = (consumerTag, message) -> {
                System.out.println(new String(message.getBody(), "UTF-8"));
                System.out.println("接收队列：" + QUEUE_NAME + "  绑定键:" + message.getEnvelope().getRoutingKey());
            };
            //接收消息
            channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
